package com.videoaggre.zcl.ui.activity.agefans

import android.content.Context
import android.view.View
import androidx.lifecycle.lifecycleScope
import com.kingja.loadsir.core.LoadService
import com.tencent.smtt.sdk.WebView
import com.videoaggre.zcl.R
import com.videoaggre.zcl.common.showErrorCallback
import com.videoaggre.zcl.common.showLoadingCallback
import com.videoaggre.zcl.common.startKtxActivity
import com.videoaggre.zcl.common.visible
import com.videoaggre.zcl.helper.InjectBundle
import com.videoaggre.zcl.helper.loadsir.LoadSirHelper
import com.videoaggre.zcl.parse.BaseParse
import com.videoaggre.zcl.ui.activity.BaseActivity
import com.videoaggre.zcl.utils.Logger
import kotlinx.android.synthetic.main.agefans_video_activity_palyer.*
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

class AgefansVideoPlayerActivity : BaseActivity(R.layout.agefans_video_activity_palyer) {
    companion object {
        private const val PLAYER_LINK = "mPlayerLink"
        fun open(context: Context, link: String) = context.startKtxActivity<AgefansVideoPlayerActivity>(value = PLAYER_LINK to link)
    }

    private val mPlayerLink: String by InjectBundle()
    private val mLoadService: LoadService<*> by lazy { LoadSirHelper.register(webview) { loadVideo() } }
    override fun initView() {
//        webview.loadUrl("https://www.agefans.tv/play/20170082?playid=4_1")
//        webview.loadUrl("file:///android_asset/agefansVideo.html")
    }

    override fun initData() {
        mLoadService.showLoadingCallback()
        loadVideo()
    }

    private fun loadVideo() {
        webview.visibility = View.INVISIBLE
        webview.mIParse = object : BaseParse(null, this, mPlayerLink) {
            override fun start() = mLoadService.showLoadingCallback()
            override fun success(html: String) {
                super.success(html)
                Logger.e(html)
            }

            override fun error() = mLoadService.showErrorCallback()
            override fun onPageFinished(webView: WebView, url: String) {
                lifecycleScope.launch {
//                webView.evaluateJavascript("""javascript:setTimeout(function () {var age_playfram = $("#age_playfram");$("body").empty();$("body").append(age_playfram);Jsbridge.html(document.getElementsByTagName('html')[0].innerHTML)},2000)""") {}
                    //Jsbridge.html(document.getElementsByTagName('html')[0].innerHTML)
                    webView.evaluateJavascript("""javascript:var age_playfram = $("#age_playfram");$("#root").remove("#age_playfram");$("#root").hide();$("body").append(age_playfram);$("body").css("background","#ffffff");Jsbridge.html(document.getElementsByTagName('html')[0].innerHTML);""") {}
                    titleView.titleStr = webView.title
                    delay(500)
                    webview.visible()
                    mLoadService.showSuccess()
                }
            }
        }
    }
}